package com.winning.bi.etl;

import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.winning.bi.jdbc.dao.Dao;
import com.winning.bi.jdbc.dao.Page;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TaskLog extends JFrame {

	final Object[] columnNames = { "任务名称", "执行时间", "起始标记", "结束标记", "数据量" };
	private static final long serialVersionUID = 1L;
	private JScrollPane scrollPane;
	private JPanel contentPane;
	JTable jtable1;
	Page page;

	Object[][] rowData;

	DefaultTableModel tableModel;

	public TaskLog(ClassPathXmlApplicationContext ac, String taskid) throws Exception {
		super("ETL日志");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Dao dao = (Dao) ac.getBean("daoTask");
		Page page1 = new Page();
		Map<String, String> map = new HashMap<String, String>();
		map.put("taskid", taskid);
		page1.setMap(map);
		setBounds(100, 100, 800, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		scrollPane = new JScrollPane();
		page = dao.queryTaskLog(page1);
		rowData = page.getStrings();
		tableModel = new DefaultTableModel(rowData, columnNames);

		jtable1 = new JTable(tableModel);
		jtable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		scrollPane.setViewportView(jtable1); // 不能直接add
		JPanel jPanel = new JPanel();

		JButton button3 = new JButton("上一页");
		button3.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				int i = page.getCurrentPage();
				
				if (i - 1 > 0) {
					page.setCurrentPage(i - 1);
					System.out.println("[上一页]"+(i-1));
				} else {
					page.setCurrentPage(1);
				}
				//rowData=page.getStrings();
				try {
					page = dao.queryTaskLog(page);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				rowData=page.getStrings();
				tableModel.setDataVector(rowData, columnNames);
				tableModel.fireTableDataChanged();
			}
		});
		JButton button5 = new JButton("下一页");
		button5.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				int i = page.getCurrentPage();
				
				page.setCurrentPage(i + 1);
				System.out.println(i+1);
				try {
					page = dao.queryTaskLog(page);
				} catch (Exception e1) {
					
				}
				if (page.getStrings().length < 1) {
					JOptionPane.showMessageDialog(contentPane, "已经是最后一页", "提示", JOptionPane.WARNING_MESSAGE);
					page.setCurrentPage(i );
					return;
				} else {
					rowData=page.getStrings();
					tableModel.setDataVector(rowData, columnNames);
					tableModel.fireTableDataChanged();
				}
			}
		});

		jPanel.add(button3);
		jPanel.add(button5);

		contentPane.add(scrollPane, BorderLayout.CENTER);
		contentPane.add(jPanel, BorderLayout.SOUTH);
		setContentPane(contentPane);

	}

}
